package accepttests;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import database.CleanPersistence;
import database.DataseConsult;
import database.query.Query;
import database.rescue.GetUserDatabase;

import sessionManager.SessionManager;
import user.User;
import user.UserIF;
import user.blog.Blog;
import user.blog.BlogIF;
import user.blog.post.Post;
import user.blog.post.comments.Comment;
import user.blog.post.comments.ManageComments;
import user.profile.Flags;
import user.profile.Profile;
import FindManager.FindBlog;

public class FindBlogFacade {
	
	FindBlog findBlog = new FindBlog();
	private List<Profile> profileList = new ArrayList<Profile>();
//	/private User user1;
	private SessionManager session ;
	//private Blog blog;
	private Post post;
	//private Comment coment;
	
	public FindBlogFacade(){
		profileList = new ArrayList<Profile>();
		session = SessionManager.getInstance();
	}
	
	public String logon(String login, String senha) throws Exception {

		return session.loginUser(login, senha);
	}

	public boolean logoff(String sessionID) throws Exception {
		return session.logoffUser(sessionID);
	}
	
	public void cleanPersistence(){
		new CleanPersistence().cleanPersistence();
	}
	
	public void createProfile(String login, String senha, String nome_exibicao,
			String email, String sexo, String dataNasc, String endereco,
			String interesses, String quem_sou_eu, String filmes,
			String musicas, String livros) throws Exception {
		
		Profile profile = new Profile(login, senha, nome_exibicao, email, sexo,
				dataNasc, endereco, interesses, quem_sou_eu, filmes, musicas,
				livros);
		verifyProfile(profile);
		profileList.add(profile);
		UserIF user1 = new User(login, senha, profile);
		user1.save();
		
	}
	
	public String createBlog(String session, String title, String description) throws Exception{
		String id = null;
		String login = DataseConsult.getLoginByUID(session);
		GetUserDatabase gud = new GetUserDatabase(login);
		UserIF user = gud.getUser();
		id = user.createBlog(session, title, description);
		user.save();
		return id;
	}
	
	public String createPost(String sessionID, String blogID, String title,
			String text) throws Exception {
		
		post = new Post(sessionID, blogID, title, text);
		// List<BlogIF>blogs = Query.findBlogs("blogid", blogID);
		// BlogIF blog = DataseConsult.getBlogByBID(blogID);

		String login = DataseConsult.getLoginByUID(sessionID);
		GetUserDatabase gud = new GetUserDatabase(login);
		UserIF user = gud.getUser();

		String p = user.getBlogByBID(blogID).addPost(post);
		//user.getBlogByBID(blogID).addPost(post);
		//System.out.println("PID1: " + p);
		//user.getBlogByBID(blogID).savePosts();
		user.save();
		return p;
		
	}
	
	public void addComment(String sessionID, String postID, String text) throws Exception{
		String bid = DataseConsult.getPostByPID(postID).getBlogId();
		GetUserDatabase gud = new GetUserDatabase(DataseConsult.getOwnerOFBlogByBID(bid));
		UserIF user = gud.getUser();
		user.getPostByPID(postID).addComment(sessionID, text);
		user.save();
		user.getPostByPID(postID).save();
	}
	
	private void verifyProfile(Profile profile) throws Exception {
		Iterator<Profile> itProfiles = profileList.iterator();
		while (itProfiles.hasNext()) {
			Profile tempProfile = itProfiles.next();

			if (profile.getPersonalProperty(Flags.EMAIL).equals(
					tempProfile.getPersonalProperty(Flags.EMAIL))) {
				throw new Exception("Email existente");
			}

			if (profile.getPersonalProperty(Flags.LOGIN).equals(
					tempProfile.getPersonalProperty(Flags.LOGIN))) {
				throw new Exception("Login existente");
			}
		}
	}
	
	public String findProfileByName(String name){
		return findBlog.getFindProfile().findProfileByName(name);
		
	}
	
	public String findProfileByInterests(String interest){
		return findBlog.getFindProfile().findProfileByInterests(interest);
	}
	
	public String findProfileByGender(String gender) throws Exception {		
		return findBlog.getFindProfile().findProfileByGender(gender);
	}
	
	public String findBlogByName(String name){
		Iterator<BlogIF> itBlogs = findBlog.getListOfBlogsByName(name).iterator();
		List<String> result = new LinkedList<String>();
		while(itBlogs.hasNext()) {
			BlogIF blog = itBlogs.next();
			result.add(blog.getBlogID());
		}
		
		//return findBlog.getListOfBlogsByName(name).toString();
		Collections.sort(result);
		return result.toString().replaceAll(" ", "");
	}
	
	public void saveData() {
		// TODO
	}
	
	public Integer getNumberOfComments(String login, String blogId) throws Exception {
		BlogIF blog = DataseConsult.getBlogByBID(blogId);
		Iterator<Post> itPosts = blog.getAllPosts().iterator();
		int count = 0;
		while(itPosts.hasNext()) {
			Post post = itPosts.next();
			Iterator<String> itCIDs = post.getComments().getCIDs().iterator();
			while(itCIDs.hasNext()) {
				String cid = itCIDs.next();
				if(post.getComments().getCommentAuthor(cid).equals(login)) {
					count++;
				}
			}
		}
		return count;
		//return findBlog.getNumberOfComments(login, blogId);
	}
}
